package com.example.yls.demoa;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnStart;
    private Button btnstop;
    private Button btnBind;
    private Button btnUnBind;
    private ServiceConnection serviceConnection;
    private MyBinder binder;
    private Button btnAdd;
    private Button btntoUpperCase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStart = findViewById(R.id.start_service);
        btnstop = findViewById(R.id.stop_service);
        btnBind = findViewById(R.id.bind_service);
        btnUnBind = findViewById(R.id.unbind_service);
        btnAdd = findViewById(R.id.all_service);
        btntoUpperCase = findViewById(R.id.binder_toUpperCase);

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.e("MainActivity","onService");
                binder = (MyBinder) iBinder;
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
              Log.e("MainActivity","onServiceDisconnected");

            }
        };

        btnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,Myservice.class);
                bindService(intent,serviceConnection,BIND_AUTO_CREATE);
            }
        });
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int result = binder.add(100,200);
                Log.e("binder.add","result = " + result);

                String str = binder.toUpperCase("hello wprld");
                Log.e("binder.toUpperCase","str = " + str);
            }
        });
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();

            }
        });

    }
}

